package com.study.buffer;

import java.nio.ByteBuffer;

/**
 * @author 朝花不迟暮
 * @version 1.0
 * @date 2020/9/27 14:07
 */
public class TestBuffer
{
    public static String str = "abcde";
    public static void main(String[] args)
    {
//        byteTest1();
        System.out.println("--------------------------------------");
        byteTest2();
    }

    /**
     * 0
     * 1024
     * 1024
     */
    public static void byteTest1(){
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        /*Returns this buffer's 位置.*/
        System.out.println(byteBuffer.position());
        /*Returns this buffer's 限制.*/
        System.out.println(byteBuffer.limit());
        /*Returns this buffer's 容量.*/
        System.out.println(byteBuffer.capacity());
    }

    /**
     * 5
     * 1024
     * 1024
     */
    public static void byteTest2(){
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        byteBuffer.put(str.getBytes());
        //因为缓存区里存放了五个数据，所以当前的位置向前走了5
        System.out.println(byteBuffer.position());
        System.out.println(byteBuffer.limit());
        System.out.println(byteBuffer.capacity());
        System.out.println("----------------flip start----------------------");
        //flip切换到读数据模式，取数据之前都得切换到读模式
        byteBuffer.flip();
        /**
         * 0
         * 5
         * 1024
         * 从上面的打印结果来看，进入只读模式后，起始位置和大小改变成了数据的大小，容量不变
         */
        System.out.println(byteBuffer.position());
        System.out.println(byteBuffer.limit());
        System.out.println(byteBuffer.capacity());
        System.out.println("----------------flip end----------------------");
        System.out.println("----------------get start----------------------");
        byte[] dst = new byte[byteBuffer.limit()];
        //得到缓存区中的数据
        byteBuffer.get(dst);
        System.out.println(new String(dst,0,dst.length));
        /**
         * 5
         * 5
         * 1024
         */
        System.out.println(byteBuffer.position());
        System.out.println(byteBuffer.limit());
        System.out.println(byteBuffer.capacity());
        System.out.println("----------------get end----------------------");
        System.out.println("----------------rewind start----------------------");
        byteBuffer.rewind();
        /**
         * 0
         * 5
         * 1024
         */
        System.out.println(byteBuffer.position());
        System.out.println(byteBuffer.limit());
        System.out.println(byteBuffer.capacity());
        System.out.println("----------------rewind end----------------------");
        System.out.println("----------------clear start----------------------");
        //清空 但是数据仍在，只是处于被遗忘的状态
        byteBuffer.clear();
        /**
         * 0
         * 1024
         * 1024
         */
        System.out.println(byteBuffer.position());
        System.out.println(byteBuffer.limit());
        System.out.println(byteBuffer.capacity());
        byteBuffer.get(dst,0,2);
        //ab
        System.out.println(new String(dst,0,2));
        System.out.println("----------------clear end----------------------");
        System.out.println("----------------mark start----------------------");
        byteBuffer.mark();//标记
        byteBuffer.get(dst,2,2);
        System.out.println(new String(dst,2,2));//cd
        System.out.println(byteBuffer.position());//4
        System.out.println("----------------mark start----------------------");
        System.out.println("----------------reset start----------------------");
        byteBuffer.reset();
        System.out.println(byteBuffer.position());//2
        System.out.println("----------------reset start----------------------");
    }
}
